import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_base_app/config/i18n/i18n.dart';

import '../../../config/app_colors.dart';
import '../../../config/i18n/i18n_key.dart';
import '../../../config/theme.dart';

abstract class IShowText {
  String showText();
}

//模仿DropdownButton
void showPopupList<T>(
  BuildContext context, {
  String tag = "showPopupList",
  double width = 150,
  double height = 40,
  double? dialogHeight,
  double dialogMaxHeight = 240,
  Alignment? alignment,
  List<T> datas = const [],
  T? selectedItem,
  ValueChanged<T>? onChanged,
}) {
  if (SmartDialog.checkExist(tag: tag)) return;

  Widget buildContent(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: List.generate(datas.length, (index) {
        final item = datas[index];
        return Material(
          color: Colors.transparent,
          child: InkWell(
            onTap: () {
              SmartDialog.dismiss();
              onChanged?.call(item);
            },
            child: Container(
              width: width,
              height: height,
              alignment: Alignment.center,
              child: selectedItem == null
                  ? Text(item is IShowText ? item.showText() : item.toString())
                  : Stack(
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(right: 30.0),
                          child: Center(
                            child: Text(
                              item is IShowText ? item.showText() : item.toString(),
                              style: TextStyle(color: datas.indexOf(selectedItem) == index ? primaryColor : null),
                            ),
                          ),
                        ),
                        // if (datas.indexOf(selectedItem) == index)
                        //   Positioned(
                        //     right: 5,
                        //     top: 0,
                        //     bottom: 0,
                        //     child: Icon(
                        //       Icons.check,
                        //       color: primaryColor,
                        //     ),
                        //   ),
                      ],
                    ),
            ),
          ),
        );
      }),
    );
  }

  Widget buildEmpty(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: Center(child: Text(I18nKey.noData.trx)),
    );
  }

  SmartDialog.showAttach(
    targetContext: context,
    alignment: alignment,
    tag: tag,
    usePenetrate: false,
    maskColor: Colors.transparent,
    nonAnimationTypes: [SmartNonAnimationType.maskClose_nonAnimation],
    builder: (_) {
      return Container(
        width: width,
        height: dialogHeight,
        constraints: BoxConstraints(maxHeight: dialogMaxHeight, minHeight: height),
        margin: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: ColorsApp.page_background,
          boxShadow: const [BoxShadow(color: Colors.black12, blurRadius: 5, spreadRadius: 0.2)],
          borderRadius: BorderRadius.circular(5),
        ),
        child: datas.isNotEmpty ? buildContent(context) : buildEmpty(context),
      );
    },
  );
}
